/*
 * example.cpp
 *
 *  Created on: 24.06.2012
 *      Author: ignacio
 */

#include "GeneticForest.h"
#include <algorithm>
using std::random_shuffle;


namespace neurignacio{

void Example()
{
	srand(time(NULL));
	time_t startTime;
	size_t dt;
	const size_t TIME_MAX=45; // seconds

	int NUMBER_OF_GENERATIONS=10000;
	const size_t NUMBERSET_SIZE=14;
	int NUMBERSET[NUMBERSET_SIZE]={1,2,3,4,5,6,7,8,9,10,25,50,75,100};
	bool ALLOW_REPETITIONS = false;
	// Now I find the goal. El objetivo es obtener, en 45 segundos, un número entero natural (del 101 al 999)
	int GOAL = rand() % 899 + 101; // Choose a number between 0 and 898 and add 101
	vector<int> numberSet(NUMBERSET,NUMBERSET+NUMBERSET_SIZE);
	int numberOfNumbers=6;
	random_shuffle(numberSet.begin(), numberSet.end());
	numberSet.resize(numberOfNumbers);
	Forest forest(numberSet, GOAL, ALLOW_REPETITIONS);
	cout << "Number Set: ";
	for (NumberContainer_t::iterator it=forest.NUMBERCONTAINER.begin(); it!=forest.NUMBERCONTAINER.end(); ++it)
		cout << *it << " ";
	cout << endl;
	cout << "Goal: " << GOAL << endl;

	// Create Father
	forest.createRandomTree();
	// Create Mother
	forest.createRandomTree();
	// Start Algorithm
	register int i=0;
	double bestScore=-1e100;
	time(&startTime);
	do
	{
		// Add some new blood
		forest.createRandomTree();
		// Find a apropiate breeding couple according to Roulette Wheel Selection
		forest.mate();
		bestScore = forest.getBestScore();
		++i;	// Generetion counter
		dt=difftime(time(NULL), startTime);	// Time counter
	}
	while (i<NUMBER_OF_GENERATIONS && bestScore!=1 && dt<TIME_MAX);
	// Print results
	cout << "Solution was " << (i==NUMBER_OF_GENERATIONS || dt>=TIME_MAX? "NOT " : "") << "found after " << i << " generations in " << dt<< " seconds" << endl;
	cout << " Fittest for goal=" << GOAL << ": "; forest.fittest->print(); cout << endl;
}


} // end neurignacio
